package com.module.service.impl;


import com.module.service.IDictService;
import com.module.dao.DictMapper;
import com.module.model.Dict;
import com.module.utils.StrKit;
import com.module.utils.ToolUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DictServiceImpl  implements IDictService {

    @Resource
    private DictMapper dictMapper;


    @Override
    public void addDict(String dictName, String dictValues) {
        //判断有没有该字典
       /* List<Dict> dicts = dictMapper.selectList(dictName,dictValues);
        if (dicts != null && dicts.size() > 0) {

        }*/

        //解析dictValues
        List<Map<String, String>> items = parseKeyValue(dictValues);

        //添加字典
        Dict dict = new Dict();
        dict.setName(dictName);
        dict.setNum(0);
        dict.setPid(0);
        this.dictMapper.insert(dict);

        //添加字典条目
        for (Map<String, String> item : items) {
            String num = item.get("KEY");
            String name = item.get("VALUE");
            Dict itemDict = new Dict();
            itemDict.setPid(dict.getId());
            itemDict.setName(name);
            try {
                itemDict.setNum(Integer.valueOf(num));
            } catch (NumberFormatException e) {

            }
            this.dictMapper.insert(itemDict);
        }
    }

    @Override
    public void editDict(Integer dictId, String dictName, String dicts) {
        //删除之前的字典
        this.delteDict(dictId);

        //重新添加新的字典
        this.addDict(dictName, dicts);
    }

    @Override
    public void delteDict(Integer dictId) {
        //删除这个字典的子词典

        dictMapper.delete(dictId);

        //删除这个词典
        dictMapper.deleteById(dictId);
    }

    @Override
    public List<Dict> selectByCode(String code) {
        return dictMapper.selectByCode(code);
    }

    @Override
    public List<Map<String, Object>> list(String conditiion) {
        return dictMapper.list(conditiion);
    }

    @Override
    public Dict selectById(Integer dictId) {
        return dictMapper.selectById(dictId);
    }

    public static List<Map<String,String>> parseKeyValue(String mutiString){
        if(ToolUtil.isEmpty(mutiString)){
            return new ArrayList<Map<String,String>>();
        }else{
            ArrayList<Map<String,String>> results = new ArrayList<Map<String,String>>();
            String[] items = StrKit.split(StrKit.removeSuffix(mutiString, ";"), ";");
            for (String item : items) {
                String[] attrs = item.split(":");
                HashMap<String, String> itemMap = new HashMap<String, String>();
                itemMap.put("KEY",attrs[0]);
                itemMap.put("VALUE",attrs[1]);
                results.add(itemMap);
            }
            return results;
        }
    }
}
